package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.core.base.enums.EnumBeanNameEnum;
import com.zxw.common.web.util.BeanUtils;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowNodesRelationModel;
import com.zxw.flow.domain.entity.FlowNodesRelationModel;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowNodesRelationModelPageVO;
import com.zxw.flow.domain.vo.FlowNodesRelationModelVO;
import com.zxw.flow.mapper.FlowNodesRelationModelMapper;
import com.zxw.flow.service.FlowNodesRelationModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowNodesRelationModelServiceImpl extends ServiceImpl<FlowNodesRelationModelMapper, FlowNodesRelationModel> implements FlowNodesRelationModelService {

    private final FlowNodesRelationModelMapper flowNodesRelationModelMapper;

    @Override
    public IPage<FlowNodesRelationModel> getPage(FlowNodesRelationModelPageVO pageVO) {
        Page<FlowNodesRelationModel> flowTypeDTOPage = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowNodesRelationModel>()
                .eq(FlowNodesRelationModel::getCompanyId, pageVO.getCompanyId())
                .eq(null != pageVO.getLineName(), FlowNodesRelationModel::getLineName, pageVO.getLineName())
                .eq(null != pageVO.getFlowTypeId(), FlowNodesRelationModel::getFlowTypeId, pageVO.getFlowTypeId())
                .eq(null != pageVO.getSourceNode(), FlowNodesRelationModel::getSourceNode, pageVO.getSourceNode())
                .eq(null != pageVO.getTargetNode(), FlowNodesRelationModel::getTargetNode, pageVO.getTargetNode());
        Page<FlowNodesRelationModel> pageData = flowNodesRelationModelMapper.selectPage(flowTypeDTOPage, wrapper);
        return pageData;
    }

    @Override
    public FlowNodesRelationModelVO add(FlowNodesRelationModelVO flowNodesRelationModelVO) {
        flowNodesRelationModelMapper.insert(flowNodesRelationModelVO);
        return flowNodesRelationModelVO;
    }

    @Override
    public FlowNodesRelationModel getFlowNodesRelationModelById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        FlowNodesRelationModel flowNodesRelationModel = flowNodesRelationModelMapper.selectOne(new LambdaQueryWrapper<FlowNodesRelationModel>()
                .eq(FlowNodesRelationModel::getId, id)
                .eq(FlowNodesRelationModel::getCompanyId, companyId)
        );
        return flowNodesRelationModel;
    }

    @Override
    public void updateBase(FlowNodesRelationModelVO flowNodesRelationModelVO) {
        flowNodesRelationModelMapper.update(flowNodesRelationModelVO, new LambdaUpdateWrapper<FlowNodesRelationModel>()
                .eq(FlowNodesRelationModel::getId, flowNodesRelationModelVO.getId())
                .eq(FlowNodesRelationModel::getCompanyId, flowNodesRelationModelVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowNodesRelationModelIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowNodesRelationModelMapper.delete(new LambdaUpdateWrapper<FlowNodesRelationModel>()
                .eq(FlowNodesRelationModel::getCompanyId, companyId)
                .in(FlowNodesRelationModel::getId, flowNodesRelationModelIds)
        );
    }
}
